﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Projektarbete
{
    class EnemyBase
    {
        Vector2 position;
        public Vector2 getPosition
        {
            get { return position; }
        }

        ushort health;
        public ushort getHealth
        {
            get { return health; }
        }
        ushort movementSpeed;
        public ushort getMovementSpeed
        {
            get { return movementSpeed; }
        }

        bool isMoving;
        public bool IsMoving
        {
            get { return isMoving; }
        }

        MovementDirection movementDirection;
        MovementDirection lastMovementDirection;

        LinkedList<MovementDirection> movementList;

        Texture2D texture;
        public Texture2D getTexture
        {
            get { return texture; }
        }

        private byte animationIndex;
        private byte animationRow;
        private float animationTime;
        private float maxAnimationTime;

        private Rectangle drawRectangle;

        public EnemyBase(Texture2D texture, Vector2 position, ushort health, ushort movementSpeed)
        {
            this.position = position;
            this.health = health;
            this.movementSpeed = movementSpeed;
            this.texture = texture;

            animationIndex = 0;
            animationRow = 0;
            animationTime = 0;

            //TODO: CHANGE MOVEMENTSPEED
            switch ((int)movementSpeed)
            {
                case 0:
                    throw new Exception("Nope");
                case 1:
                case 2:
                    maxAnimationTime = 2;
                    break;
                case 3:
                    maxAnimationTime = 1;
                    break;
                default:
                    maxAnimationTime = 0.5f;
                    break;
            }

            isMoving = false;

            movementDirection = MovementDirection.none;
            lastMovementDirection = MovementDirection.none;

            drawRectangle = new Rectangle(0, 0, 24, 24);

            movementList = new LinkedList<MovementDirection>();
        }

        public void Hurt(ushort amount)
        {
            health -= amount;
            KillCheck();
        }

        public void Hurt(int amount)
        {
            health -= (ushort)amount;
            KillCheck();
        }

        public void Heal(ushort amount)
        {
            health += amount;
        }

        public void Heal(int amount)
        {
            health += (ushort)amount;
        }

        public void KillCheck()
        {
            if (health <= 0)
            {
                AIManager.KillEnemy(this);
            }
        }

        private void Animate()
        {
            animationTime++;

            if (animationTime > maxAnimationTime)
            {
                animationTime = 0;
                animationIndex++;

                if (animationIndex > 7)
                    animationIndex = 0;
            }

            drawRectangle.X = animationIndex * 24;
        }

        private void RoundAnimation()
        {
            if (animationIndex == 3 || animationIndex == 5)
                animationIndex = 4;
            else
                animationIndex = 0;

            drawRectangle.X = animationIndex * 24;
        }

        public void Update(Vector2 playerPosition)
        {
            if (isMoving)
            {
                Move();
            }
            else
            {
                if (movementList.Count > 0)
                {
                    movementDirection = movementList.First();
                    movementList.RemoveFirst();
                    isMoving = true;
                    Move();
                }
                if (Input.IsNewPress(Microsoft.Xna.Framework.Input.Keys.H))
                {
                    movementList = Pathfinding.AStar(Global.ToTilePosition(position), Global.ToTilePosition(playerPosition));
                }
            }
        }

        public void Move()
        {
            switch (movementDirection)
            {
                case MovementDirection.up:
                    position.Y -= movementSpeed;
                    Animate();
                    if (AIManager.HitTestPlayer(this))
                        AIManager.CombatTime();

                    if (position.Y % 24 == 0)
                    {
                        this.movementDirection = MovementDirection.none;
                        isMoving = false;
                    }
                    break;
                case MovementDirection.down:
                    position.Y += movementSpeed;
                    Animate();

                    if (AIManager.HitTestPlayer(this))
                        AIManager.CombatTime();

                    if (position.Y % 24 == 0)
                    {
                        this.movementDirection = MovementDirection.none;
                        isMoving = false;
                    }
                    break;
                case MovementDirection.left:
                    position.X -= movementSpeed;
                    animationRow = 1;
                    drawRectangle.Y = animationRow * 24;
                    Animate();
                    if (AIManager.HitTestPlayer(this))
                        AIManager.CombatTime();

                    if (position.X % 24 == 0)
                    {
                        this.movementDirection = MovementDirection.none;
                        isMoving = false;
                    }
                    break;
                case MovementDirection.right:
                    position.X += movementSpeed;
                    animationRow = 0;
                    drawRectangle.Y = animationRow * 24;
                    Animate();
                    if (AIManager.HitTestPlayer(this))
                        AIManager.CombatTime();

                    if (position.X % 24 == 0)
                    {
                        this.movementDirection = MovementDirection.none;
                        isMoving = false;
                    }
                    break;
                default:
                    isMoving = false;
                    break;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, drawRectangle, Color.White);
        }
    }
}
